package user

import (
	"github.com/jinzhu/gorm"
	"webGoDemo/model"
)

/**
dao 层
*/
// UserResource is the REST layer to the User domain

//type UserRepo struct {
//	// normally one would use DAO (dao access object)
//	users *[3]model.User
//}

type Repo struct {
	// normally one would use DAO (dao access object)
	db *gorm.DB
}

//构造方法

//func NewUserRepo(usersList *[3]model.User) *UserRepo {
//	userRepos := new(UserRepo)
//	userRepos.users = usersList
//	logs.Info().Interface("userRepos", userRepos)
//	return userRepos
//}

func NewUserRepo(db *gorm.DB) *Repo {
	return &Repo{
		db: db,
	}
}

func (u *Repo) FindAllUsers() ([]*model.User, error) {
	var users []*model.User
	err := u.db.
		Table("user").
		Where("username is not null").
		Where("id<>0").
		Limit(10).
		Joins("").
		Find(&users).Error
	if err != nil {
		return nil, err
	}
	return users, nil
}

func (u *Repo) GetUserById() (*model.User, error) {
	return nil, nil
}
